<?php

class MenuAction extends PublicAction
{
    protected $menuModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->menuModel = D('Menu');
    }

    public function index()
    {
        //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like', '%' . $keyword . '%');
        $map['id|name'] = array($condition, $condition, '_multi' => true); //搜索条件

        //获取所有菜单
        $map['status'] = array('egt', '0'); //禁用和正常状态

        $data_list = $this->menuModel->where($map)->order('listorder asc, id asc')->select();

        import('@.ORG.Tree');
        $tree = new Tree();
        $data_list = $tree->toFormatTree($data_list, 'name', 'id', 'parentid');

        $this->assign('menu_list', $data_list);

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }

    // 添加菜单
    function add()
    {
        if (IS_POST) {

            if (false === $this->menuModel->create()) {
                $this->error($this->menuModel->getError());
            }

            $pid = I('get.parentid', 0, 'intval');
            $id = $this->menuModel->add();

            if ($id !== false) {

                saveCache('Menu');

                $this->assign('jumpUrl', U('Menu/index', 'pid=' . $pid));
                $this->success('添加成功！');
            } else {
                $this->error('提交失败！: ' . $this->menuModel->getDbError());
            }
        } else {
            $parentid = I('get.parentid', 0, 'intval');

            import('@.ORG.Tree');
            $result = $this->menuModel->select();
            foreach ($result as $r) {
                $r['selected'] = $r['id'] == $parentid ? 'selected' : '';
                $array[] = $r;
            }

            $str = "<option value='\$id' \$selected>\$spacer \$name</option>";

            $tree = new Tree($array);
            $tree->icon = array('│ &nbsp;&nbsp;&nbsp;', '├─ ', '└─');
            $select_categorys = $tree->get_tree(0, $str, $parentid);
            $this->assign('select_categorys', $select_categorys);
            $this->display();
        }

    }

    // 编辑菜单
    function edit()
    {
        if (IS_POST) {

            if (false === $this->menuModel->create()) {
                $this->error($this->menuModel->getError());
            }

            if (false !== $this->menuModel->save()) {

                saveCache('Menu');

                $this->success('修改成功');
            } else {
                $this->success('修改失败!: ' . $this->menuModel->getDbError());
            }
        } else {
            $id = I('get.id', 0, 'intval');

            $vo = $this->menuModel->find($id);
            $parentid = $vo['parentid'];
            import('@.ORG.Tree');
            $result = $this->Menu;
            foreach ($result as $r) {
                $r['selected'] = $r['id'] == $parentid ? 'selected' : '';
                $array[] = $r;
            }
            $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
            $tree = new Tree($array);
            $tree->icon = array('│ &nbsp;&nbsp;&nbsp;', '├─ ', '└─');
            $select_categorys = $tree->get_tree(0, $str, $parentid);
            $this->assign('select_categorys', $select_categorys);

            $this->assign('vo', $vo);

            $this->display();
        }
    }

    //删除
    function delete()
    {
        $id = I('get.id', 0, 'intval');

        if ($id) {
            $strChildId = $this->menuModel->getStrChildId($id);
            if (false !== $this->menuModel->delete($strChildId)) {
                saveCache('Menu');

                $this->success('删除成功！');
            } else {
                $this->error('删除失败！: ' . $this->menuModel->getDbError());
            }
        } else {
            $this->error('缺少必要的参数!');
        }
    }

    // 批量删除
    function deleteall()
    {
        $ids = $_POST['ids'];

        if (!empty($ids) && is_array($ids)) {

            $id = implode(',', $ids);
            if (false !== $this->menuModel->delete($id)) {
                saveCache('Menu');
                $this->success('删除失败！');
            } else {
                $this->error('删除失败！: ' . $this->menuModel->getDbError());
            }
        } else {
            $this->error('缺少必要的参数!');
        }
    }

    /*状态*/
    public function status()
    {
        if ($this->menuModel->save($_GET)) {
            saveCache('Menu');
            $this->success('提交成功!');
        } else {
            $this->error('提交失败！');
        }
    }
}